'use strict';

angular.module('CBO').controller('MOController',
    ['$scope', '$stateParams', 'PublicModule', 'PublicFunction', '$location', 'Authentication',
    function($scope, $stateParams, PublicModule, PublicFunction, $location, Authentication) {
        //$scope.authentication = Authentication;
        
        $scope.btnSave = function(){
            if($scope.Module.MO.FocusedRecord.Code === undefined || 
                $scope.Module.MO.FocusedRecord.Code.trim() === ""){
                PublicFunction.alert("编码不能为空！", "保存错误");
                return;
            }
            if($scope.Module.MO.FocusedRecord.ItemMaster._id === undefined || 
                $scope.Module.MO.FocusedRecord.ItemMaster._id === null){
                PublicFunction.alert("物料不能为空！", "保存错误");
                return;
            }

            $scope.Module.MO.save();
        };
        $scope.btnCancel = function(){
            $scope.Module.MO.cancel();            
        };
        $scope.btnAdd = function(){
            $scope.Module.MO.add();
        };
        $scope.btnDelete = function(){
            $scope.Module.MO.delete();
        };
        $scope.btnList = function(){
            $location.url('/index/MO/List');
        };
        $scope.btnFirstPage = function(){
            $scope.Module.MO.firstPage();
        };
        $scope.btnPrePage = function(){
            $scope.Module.MO.prePage();
        };
        $scope.btnNextPage = function(){
            $scope.Module.MO.nextPage();
        };
        $scope.btnLastPage = function(){
            $scope.Module.MO.lastPage();
        };

        //初始化
        PublicModule.initModule($scope, 'MO');
        $scope.Module.MO.QueryParameter.Populate = {path: 'ItemMaster', select: 'Code Name'};

        if($stateParams.id) {
            $scope.Module.MO.findById($stateParams.id, function(record){
                $scope.Module.MO.FocusedRecord = record;
            });
        }
        else{
            $scope.btnAdd();
        }
    }
]);
